<?php
include_once ( "AbstractDAO.php" );

class ShopDAO extends  AbstractDAO 
{
	
	private $shopSearchEngine;
	
	public function __construct()
    {
    	parent::__construct();
    	$this->shopSearchEngine = GetShopSearch();
    	
    }
	
	function GetShopInfo($sid)
	{
		$sql = "select si.*, sp.plat_name from shop_info si, shop_platform sp 
		where si.status<>9 and si.plat_id=sp.id and si.id=".$sid;	
		$result = $this->db->GetRow( $sql );
		return $result;
	} 
	
	function GetShopInfoPlus($platform,$sid)
	{	
		$sql="select * from shop_info_".$platform." where shop_id=".$sid;
		$result = $this->db->GetRow( $sql );
    	return $result;	 
	}

	function GetShopInfoPlusByExtID($platform,$extID)
	{	
		$sql="select si.id,si.shop_name, sip.".$platform."_id from shop_info si , shop_info_".$platform." sip where si.id = sip.shop_id and sip.".$platform."_id=".$extID;
		$result = $this->db->GetRow( $sql );
    	return $result;	 
	}
		
	function GetShopByUid($uid, $count = 20 )
	{
		$ids = $this->shopSearchEngine->GetShopByUid( $uid, $count );
		
		//$sql="select * from shop_info where status<>9 and create_user_id=".$uid." order by id desc limit 0,".$count;
		if( $ids == false )
			return false;
			
		$sql="select * from shop_info where id in(" . implode(',', $ids) . ")";
		 	
		return $this->SortByIdArray( $this->db->GetPlan($sql), $ids, "id" );
	}
	
	function GetShopName($sid)
	{
		return $this->db->GetValue("select shop_name from shop_info where status<>9 and id=".$sid);
	}
	
	function GetNickName($sid)
	{
		return $this->db->GetValue("select nick from shop_info where status<>9 and id=".$sid); 
	}
		
	function GetPlatID($sid)
	{
		return $this->db->GetValue("select plat_id from shop_info where status<>9 and id=".$sid);
	}
	
	function GetShopStatus($sid)
	{
		
		return $this->db->GetValue("select status from shop_info where id=".$sid);
		
	}
	
	function GetShopPlatform($platid)
	{
		$sql = "select plat_name from shop_platform where id=$platid";
		return $this->db->GetValue($sql);
	}

	function GetShopPlatCode($platid)
	{
		$sql = "select plat_code from shop_platform where id=$platid";
		return $this->db->GetValue($sql);
	}
	
	function GetShopFeatures($sidList)
	{
		  /**
		  * Store Name instead of id
		  */
		if(!edp_is_empty_string($sidList))
		{
			
			$sql = "select id,feature_name from shop_feature where id in ($sidList)";
			$featureArr = $this->db->GetPlan($sql);	
		    return $featureArr;
		}
		else	
		    return false;		
	}		
	
	function GetShopPayMethods($sidList)
	{
	  /**
	  * Store Name instead of id
	  */		
		if(!edp_is_empty_string($sidList)){
		
			$sql = "select id,method_name from shop_paymethod where id in ($sidList)";
		    $paymethodArr = $this->db->GetPlan($sql);
		    return $paymethodArr;
		    
		}else{
			
		    return false;		
		}
	}
	
	function GetShopTags($tag)
	{
		if(!edp_is_empty_string($tag)){

		  $tagArr = explode(" ", $tag);
		  return $tagArr;
		
		}else{
			
		  return false; 
		
		}
	}
	
	function GetShopIDByURL($shopURL)
	{
		 return $this->db->GetValue("select id from shop_info where shop_url='$shopURL'");
	}
	
	function GetShopIDByNick($nickName,$platID){
		
		return $this->db->GetValue("select id from shop_info where nick = '$nickName' and plat_id = '$platID'");
	}
		
	function GetShopIDByExtID($extShopID,$platform)
	{	
		$sql = "select shop_id from shop_info_".$platform." where ".$platform."_id='$extShopID'";
		 
		return $this->db->GetValue($sql);
	}

	function GetShopInfoByNick($nickName){
		
		return $this->db->GetRow("select id,shop_name from shop_info where nick = '$nickName'");
	}	
	
	
	function GetShopCount($platid)
	{
		return $this->shopSearchEngine->GetShopCount( $platid );
		//return $this->db->GetValue("select count(*) from shop_info where status<>9 and plat_id=".$platid);
	}
	
	function GetHotShopByPlatid( $platid, $count = 12)
	{
		$cacheManager = GetCacheManager();
		$re = $cacheManager->Get( "hotShopByPlatId_".$platid."_".$count, CNS_PRODUCT_SHOP_VARIABLE );
		
		if( $re!= false )
			return $re;
		
		$searchEngine = GetShopSearch();
		$ids = $searchEngine->GetHotShopByPlatId( $count, $platid );
		
		if( $ids === false )
			return false;
			
		$sql = "select id, shop_name, logo_url, review_count, score from shop_info where id in(" . implode(',', $ids) . ")";
		
		return $this->SortByIdArray( $this->db->GetPlan($sql, true, CNS_PRODUCT_SHOP_VARIABLE, "hotShopByPlatId_".$platid."_".$count), $ids );
	}

	function GetShopByCreatetime($platid, $count=12)
	{
		$cacheManager = GetCacheManager();
		$re = $cacheManager->Get( "shopByCreatetime_".$platid."_".$count, CNS_PRODUCT_SHOP_VARIABLE );
		
		if( $re!= false )
			return $re;
			
		$sql="select id, shop_name, logo_url from shop_info where status<>9 and plat_id=".$platid." order by create_time desc limit 0,".$count;
		return $this->db->GetPlan($sql, true, CNS_PRODUCT_SHOP_VARIABLE, "shopByCreatetime_".$platid."_".$count);
	}
	
	public function GetShopByNum($num){
		
		$num1 = ($num-1)*10000+1;
		$num2 = $num*10000;
		$sql = "select id , shop_name from shop_info where id > ".$num1." and id < ".$num2;
		return $this->db->GetPlan($sql);
	}	
		
	function UpdateShopViewCount($sid)
	{	
		$sql = "update shop_info set view_count = view_count +1 where id=".$sid;
		$this->db->Update($sql);
	}
		
	function UpdateShopViewHistory($userip,$sid)
	{ 	
	 	//$nowday = date("Y-m-d");	
	 	$now=GetCurrentDate();
	 	$status = $this->db->GetValue("select * from shop_view_history where user_ip = " . ip2long($userip) . " and shop_id = $sid"); 	
	 	if($status==false)
	 	{
		 	$sql = "insert into 
					shop_view_history(user_ip, shop_id, date) values( " . ip2long($userip) . ", $sid, '" . GetCurrentDate() ."')";	
			$this->db->Insert( $sql );	
		}
		else
		{
		   	$sql = "update shop_view_history set date='" . GetCurrentDate() . "' where user_ip = " . ip2long($userip) . " and shop_id = $sid";	
			$this->db->Update( $sql );	
		}
	 }
	 
	/**
	 * Need SQL Optimization!
	 * @return also viewed shop
	 */
	function GetAlsoViewedShopList($sid,$count = 6)
	{
		$cacheManager = GetCacheManager();
		$re = $cacheManager->Get( "alsoViewedShopList_".$sid."_".$count, CNS_PRODUCT_SHOP_VARIABLE );
		
		if( $re!= false )
			return $re;

	  	$sql = " select shop_id from shop_view_history a , (
							select distinct user_ip
							from shop_view_history
							where shop_id = $sid
							order by date desc
						 ) b
							where a.user_ip = b.user_ip
							and a.shop_id <> $sid order by date desc limit 0,$count" ;

		$sidArry = $this->db->GetCol($sql);
		if(!$sidArry)
			return false;
		else
		{
			$sidList=implode(',', $sidArry);
			$sql="select id, shop_name, shop_url,score,review_count from shop_info where status<>9 and id in (".$sidList.")";
	    	return $this->db->GetPlan($sql, true, CNS_PRODUCT_SHOP_VARIABLE, "alsoViewedShopList_".$sid."_".$count);
		}
	}
	 
	function GetHotShopSameCategory($sid, $platid, $count = 5 )
	{
		$cacheManager = GetCacheManager();
		$re = $cacheManager->Get( "hotShopSameCategory_".$sid."_".$platid."_".$count, CNS_PRODUCT_SHOP_VARIABLE );
		
		if( $re!= false )
			return $re;
			
		$count = intval( $count );
		$scid=$this->db->GetValue("select cate_id_list from shop_info where id=".$sid);
		if(!$scid)
			return false;
		else
		{
			$searchEngine = GetShopSearch();
			$ids = $searchEngine->GetHotShopWithSameCat( $scid, $platid, $count + 1 );
			
			if( $ids === false )
				return false;
				
			foreach( $ids as $key => $value )
			{
				if( $value == $sid )
				{
					unset( $ids[$key] );
					break;
				}
			}
			if( count($ids) == 0 )
				return false;
			
			if( count($ids) > $count ) 
				array_splice( $ids, $count);
				
			
			$sql = 'select id, shop_name, shop_url, score, review_count from shop_info 
					where id in(' . implode(',', $ids) . ')';
			
			return $this->db->GetPlan( $sql, true,  CNS_PRODUCT_SHOP_VARIABLE, "hotShopSameCategory_".$sid."_".$platid."_".$count);
			
			/*
			if($platid<'100')
			{
				
				$sql="select id, shop_name, shop_url, score, review_count from shop_info 
				where status<>9 and plat_id<100 and cate_id_list in ('$scid') and id<>$sid order by review_count desc limit 0,$count";
				return $this->db->GetPlan($sql);
			}
			else
			{
				$scidArr = explode(",", $scid);
				if(count($scidArr)=='1')
				{
					$sql="select id, shop_name, shop_url, score, review_count from shop_info 
					where status<>9 and plat_id>=100 and FIND_IN_SET('$scidArr[0]',cate_id_list)>0 and id<>$sid order by review_count desc limit 0,$count";
				}
				else
				{
					$sql="select id, shop_name, shop_url, score, review_count from shop_info 
					where status<>9 and plat_id>=100 and (FIND_IN_SET('$scidArr[0]',cate_id_list)>0";
					for($i='1'; $i<count($scidArr); $i++)
					{
						$sql=$sql." or FIND_IN_SET('$scidArr[$i]',cate_id_list)>0";
					}
					$sql=$sql.") and id<>$sid order by review_count desc limit 0,$count";
				}
				return $this->db->GetPlan($sql);
			}
			*/
		}
	}  
	  		
	/**
	 * Insert Shop data
	 */
	function InsertCommonShopData($platid,$shopName,$shopURL,$logoURL,$nickName,$createdDate,
								  $shopLocation,$userID,$status,$shopCategory,$shopDesc){

		$now=GetCurrentDate();
		
		$sql = "insert into 
				shop_info( plat_id, shop_name, shop_url, logo_url, nick, created, location, description,
								 status, create_user_id, create_time, cate_id_list) 
				values( $platid, '$shopName', '$shopURL', '$logoURL', '$nickName', '$createdDate', '$shopLocation', '$shopDesc',
						$status, $userID, '$now', '$shopCategory' ) ";
		
		$this->db->Insert( $sql );		
		
		$id = $this->db->LastID();	

		return $id;
	
	}
	
	
	function InsertTaobaoShopData($platid,$shopName,$shopURL,$logoURL,$nickName,$createdDate,$shopLocation,$userID,
								  $status,$shopCategory,$shopDesc,$shopID,$commissionURL,$commissionRate,$isMall)
	{
		try
		{
			$this->db->BeginTransaction();		

			$now=GetCurrentDate();
										  
			$id = $this->InsertCommonShopData($platid,$shopName,$shopURL,$logoURL,$nickName,$createdDate,
									          $shopLocation,$userID,$status,$shopCategory,$shopDesc);
			$sql = " insert into shop_info_taobao values( $id, '$shopID', '$commissionURL', '$commissionRate', $isMall, 'NOW()' ) ";
			$this->db->Insert( $sql );
		}
		catch( Exception $e)
	    {
	    	$this->db->Rollback();
	    	$id='';
	    	return false;
	    }	
	    $this->db->Commit();
		return $id;	  								  	
	}
					    
	function InsertEachnetShopData($platid,$shopName,$shopURL,$logoURL,$nickName,$createdDate,$shopLocation,$userID,
								   $status,$shopCategory,$shopDesc,$shopID)
	{
		try
		{
			$this->db->BeginTransaction();
			
			$now=GetCurrentDate();
				
			$id = $this->InsertCommonShopData($platid,$shopName,$shopURL,$logoURL,$nickName,$createdDate,
									          $shopLocation,$userID,$status,$shopCategory,$shopDesc);
			$sql = " insert into shop_info_eachnet values( $id, '$shopID') ";
			$this->db->Insert( $sql );	
		}
		catch( Exception $e)
	    {
	    	$this->db->Rollback();
			$id='';
	    	return false;
	    }	
	    $this->db->Commit();
		return $id;	  	
								   	
	}

	function InsertPaipaiShopData($platid,$shopName,$shopURL,$logoURL,$nickName,$createdDate,$shopLocation,$userID,
								  $status,$shopCategory,$shopDesc,$shopID)
	{
		try
		{
			$this->db->BeginTransaction();	
			
			$now=GetCurrentDate();
										  	
			$id = $this->InsertCommonShopData($platid,$shopName,$shopURL,$logoURL,$nickName,$createdDate,
									          $shopLocation,$userID,$status,$shopCategory,$shopDesc);
			$sql = " insert into shop_info_paipai values( $id, '$shopID') ";
			$this->db->Insert( $sql );	
		}
		catch( Exception $e)
	    {
	    	$this->db->Rollback();
	    	$id='';
	    	return false;
	    }	
	    $this->db->Commit();
		return $id;	 
								  	
	}
	
	function InsertYouaShopData($platid,$shopName,$shopURL,$logoURL,$nickName,$createdDate,$shopLocation,$userID,
								$status,$shopCategory,$shopDesc,$shopID)
	{
		try
		{
			$this->db->BeginTransaction();										
			
			$now=GetCurrentDate();
						
			$id = $this->InsertCommonShopData($platid,$shopName,$shopURL,$logoURL,$nickName,$createdDate,
									          $shopLocation,$userID,$status,$shopCategory,$shopDesc);
			$sql = " insert into shop_info_youa values( $id, '$shopID',0) ";
			$this->db->Insert( $sql );	
		}
		catch( Exception $e)
	    {
	    	$this->db->Rollback();
	    	$id='';
	    	return false;
	    }	
	    $this->db->Commit();
		return $id;	 						
	}
	
	
	function UpdateShopData($sid,$uid,$shopName,$logoURL,$shopDesc){

		$now=GetCurrentDate();
		
		$sql = "update shop_info set shop_name='".$shopName."', logo_url='".$logoURL."', description='".$shopDesc."',
								 modify_user_id =".$uid.", modify_time='".$now."' where id=".$sid; 
		
		$this->db->Update( $sql );		
		
	}	
	
	function UpdateTaobaoPlusData($sid,$commission_rate,$click_url){


		$sql = "update shop_info_taobao set click_url='".$click_url."', commission_rate='".$commission_rate."' where shop_id=".$sid; 
		
		$this->db->Update( $sql );		
		
	}	
	
	
	//////////////////////////////////////////////////////////////////////
	// search related database functions
	/**
	 *
	 * @param <type> $ids
	 * @return <type>
	 */
	public function GetShopInfoByIdList( $ids, $platid="" )
	{
		$result = array();

		if( is_array( $ids ) && count($ids) > 0 ){
		$strIds = implode( ",", $ids );

		//!!!!shit here, need to sort the result with extra sql statement : ORDER BY FIELD(id, ".$strIds.")
		//and then do sort here
		if( $platid == "" )
			$sql = "select *, (select name from citylist where id=s.location) as location_text, (select cate_name from shop_category where id=s.cate_id_list) as cate_name from shop_info s where s.status<>9 and s.id in (".$strIds.") ORDER BY FIELD(s.id, ".$strIds.")";
		else
			$sql = "select *, (select name from citylist where id=s.location) as location_text, (select cate_name from shop_category where id=s.cate_id_list) as cate_name from shop_info s where s.status<>9 and s.id in (".$strIds.") and plat_id=$platid ORDER BY FIELD(s.id, ".$strIds.")";
		
		$rows = $this->db->GetPlan( $sql );

		foreach( $rows as $row ){
			if( !edp_is_empty_string( $row['tag'] ) ){
				$row['tags'] = array_filter( explode( " ", $row['tag'] ));
			}
			//set the default value to false
			else{
				$row['tags'] = false;
			}

			$row['plat_name'] = $this->GetShopPlatform( $row['plat_id'] );

                        if( strpos( $row['logo_url'], "http" ) === false ){
                            $row['logo_url'] = STATIC_DOMAIN.$row['logo_url'];
                        }
			$result[] = $row;
		}
		}

		return $result;
	}

	/**
	 * get top shop detail information
	 *
	 * @param string $orderby
	 * @param int $perpage
	 * @param int $cpage
	 * @return array
	 */
	public function GetTopShopList( $sortby, $platform, $perpage, $cpage=1 )
	{
		$valid_sortby = array("score", "reality", "service", "velocity", "rate", "review_count", "view_count");
		
		if( in_array( $sortby, $valid_sortby ) || $sortby == "")
		{
			$sortby = $sortby == "" ? "review_count" : $sortby;
			
			if( empty ($platform) )
				$platform = null;
			
			$ids = $this->shopSearchEngine->GetTopShopList( $sortby, $platform, ($cpage-1) * $perpage, $perpage );
			if( is_array( $ids ) && count( $ids ) > 0 ){
				$strIds = implode(',', $ids);
			
				$sql = "select * from shop_info where id in( $strIds ) order by field(id, $strIds)";
				return $this->db->GetPlan( $sql );
			}
			else{
				return false;
			}
		}
		else {
			$platCondition = empty ($platform) ?  "" : " where plat_id = $platform ";
			$limitCondition = "LIMIT " . ($cpage-1) * $perpage . ", " . $perpage;
			
			if( strstr( $sortby, "create_time" ) || strstr( $sortby, "review_count" ) ){
				$orderby = strstr( $sortby, "create_time" ) !== false ? "create_time" : "review_count";
				$arrs = explode("-",$sortby);
				if( count($arrs) == 2 ){
					
					$sortby = $arrs[0];
					$interval = intval($arrs[1]);
					
					$sql = " select * from shop_info inner join( select id from shop_info $platCondition";

                    if( strstr( $sql, "where") !== false ){
                        $sql = $sql . " and status<>9 and DATEDIFF(NOW(),create_time) < $interval order by $orderby desc $limitCondition) t on(t.id = shop_info.id)";
                    }
                    else{
                        $sql = $sql . " where status<>9 and DATEDIFF(NOW(),create_time) < $interval order by $orderby desc $limitCondition) t on(t.id = shop_info.id)";
                    }
                    return $this->db->GetPlan( $sql);
                    
				}
			}
		}
		
		//return $this->getRows($sql, $perpage, $cpage);
	}

	/**
	 * get top shop count
	 * @param string $orderby
	 * @return int
	 */
	public function GetTopShopListCount( $sortby, $platform ){
		
		if( strstr( $sortby, "create_time" ) || strstr( $sortby, "review_count" ) ){
			$orderby = strstr( $sortby, "create_time" ) !== false ? "create_time" : "review_count";
			$arrs = explode("-",$sortby);
			if( count($arrs) == 2 ){
				$sortby = $arrs[0];
				$interval = intval($arrs[1]);
				$sql = "select count(*) 
				          from shop_info
				         where status<>9 
				           and DATEDIFF(NOW(),create_time) < $interval
				      order by $orderby desc";
				return $this->db->GetValue( $sql);
			}
		}
		else{
			$sortby = $sortby == "" ? "review_count" : $sortby;
			return $this->shopSearchEngine->GetTopShopListCount( $sortby, $platform );
		}
		
		/*
		$sql = "select count(*) from shop_info where status<>9"; //order by bla bla bla.... TODO
        if( !empty ($platform) ){
            $sql = $sql." and plat_id=$platform";
        }

		$result = $this->db->GetValue($sql);

		if( $result )
			return $result;
		else
			return '0';
		*/
	}

    /**
     * get top_detail shop ids from ranking table
     *
     * @param enum $sortbydetail
     * @param int $perpage
     * @param int $cpage
     * @return array
     */
    public function GetTopShopIdsFromRanking( $sortbydetail, $perpage, $cpage=1 )
	{
		$sql = "select shop_id from ranking where ".$sortbydetail."!=0";
	
        return $this->getRows($sql, $perpage, $cpage, "GetCol");
	}


    /**
     *
     * @param enum $sortbydetail
     * @return int
     */
    public function GetTopShopIdsFromRankingCount( $sortbydetail, $platid="", $idlist=array() ){
		$sql = "select count(*) from ranking where ".$sortbydetail."!=0";

		if( $platid != "" ){
			if( is_array( $idlist ) ){
			$strIds = implode( ",", $idlist );
			//and then do sort here
			$sql = "select count(*) from shop_info s where s.status<>9 and plat_id=$platid and s.id in (".$strIds.")";
			}
		}

		$result = $this->db->GetValue($sql);

		if( $result )
			return $result;
		else
			return '0';
	}
	
	/**********************************************************
	 *	Get all the tags of the shop
	 *	Seal Created by 2009-12-14
	 **********************************************************/
	public function GetTags( $sid )
	{
		$sql = "select tag
				from shop_info
				where id = $sid ";
		return  $this->db->GetValue( $sql );
	}
	
	/**********************************************************
	 *	Used for search in the user's list
	 *	Seal Created by 2010-1-31
	 **********************************************************/
	public function GetShopSimpleInfo( $ids )
	{
		if( $ids === false )
			return false;
			
		$result = array();
		$strIds = implode( ",", $ids );
		
		return  $this->db->GetPlan( 
			"select si.id, si.shop_name,si.plat_id,sp.plat_name, si.nick,si.score,si.reality, si.service, si.velocity, si.rate, si.logo_url
			 from shop_info si, shop_platform sp 
			 where si.status<>9 and si.id in ($strIds) and si.plat_id=sp.id "
		);

	}
	
	public function GetShopForValidation( $ids )
	{
		$sql = "select id, plat_id, logo_url
				from shop_info
				where id in ($ids)";
		
		return $this->db->GetPlan( $sql );
	}
	
	public function UpdateFirstReviewer($sid, $uid, $date)
	{
		$sql = "select review_count from shop_info where id=$sid";
		$count = $this->db->GetValue( $sql );
		if( $count===false||$count>'1' )//add review之后调用，所以为>1
			return false;
		else
		{
			$sql="update shop_info set first_user_id=$uid, first_time='$date' where id=$sid";
			$this->db->Update( $sql );
			return true;
		}
	}
	
	public function GetPayMethod()
	{
		$sql = "select * from shop_paymethod";
		return $this->db->GetPlan( $sql, true, CNS_SLOW_CHANGE, 'getPayMethod' );
	}
	
	public function GetFeature()
	{
		$sql = "select * from shop_feature";
		return $this->db->GetPlan( $sql, true, CNS_SLOW_CHANGE, 'getShopFeature' );
	}
	
	public function GetBestScoreShop( $count = 10 )
	{
		$cacheManager = GetCacheManager();
		$re = $cacheManager->Get( 'getBestScoreShop_'.$count, CNS_INDEX );
		
		if( $re!= false )
			return $re;
		
		$searchEngine = GetShopSearch();
		$ids = $searchEngine->GetBestScoreShop( $count );
		
		if( $ids === false )
			return false;
			
		$sql = "select id, shop_name, logo_url, score from shop_info where id in(" . implode(',', $ids) . ")";
		
		return $this->SortByIdArray( $this->db->GetPlan($sql, true, CNS_INDEX, 'getBestScoreShop_'.$count), $ids );
	}
	
	public function GetBestScoreMall( $count = 10 )
	{
		$cacheManager = GetCacheManager();
		$re = $cacheManager->Get( 'getBestScoreMall_'.$count, CNS_INDEX );
		
		if( $re!= false )
			return $re;
		
		$searchEngine = GetShopSearch();
		$ids = $searchEngine->GetBestScoreMall( $count );
		
		if( $ids === false )
			return false;
			
		$sql = "select id, shop_name, logo_url, score from shop_info where id in(" . implode(',', $ids) . ")";
		
		return $this->SortByIdArray( $this->db->GetPlan($sql, true, CNS_INDEX, 'getBestScoreMall_'.$count), $ids );
	}	
	
	public function GetBestRateShop( $count = 10 )
	{
		$cacheManager = GetCacheManager();
		$re = $cacheManager->Get( 'getBestRateShop_'.$count, CNS_INDEX );
		
		if( $re!= false )
			return $re;
		
		$searchEngine = GetShopSearch();
		$ids = $searchEngine->GetBestRateShop( $count );
		
		if( $ids === false )
			return false;
			
		$sql = "select id, shop_name, logo_url, score, rate from shop_info where id in(" . implode(',', $ids) . ")";
		
		return $this->SortByIdArray( $this->db->GetPlan($sql, true, CNS_INDEX, 'getBestRateShop_'.$count), $ids );
	}
	
	public function GetBestRateMall( $count = 10 )
	{
		$cacheManager = GetCacheManager();
		$re = $cacheManager->Get( 'getBestRateMall_'.$count, CNS_INDEX );
		
		if( $re!= false )
			return $re;
			
		$sql="select id, shop_name, logo_url from shop_info where status=1 and plat_id>=100 order by rate desc limit 0,".$count;
		return $this->db->GetPlan($sql, true, CNS_INDEX, 'getBestRateMall_'.$count);
	}

	public function GetBestServiceShop( $count = 10 )
	{
		$cacheManager = GetCacheManager();
		$re = $cacheManager->Get( 'getBestServiceShop_'.$count, CNS_INDEX );
		
		if( $re!= false )
			return $re;
			
		$sql="select id, shop_name, logo_url from shop_info where status=1 and plat_id<100 order by service desc limit 0,".$count;
		return $this->db->GetPlan($sql, true, CNS_INDEX, 'getBestServiceShop_'.$count);
	}
	
	public function GetBestServiceMall( $count = 10 )
	{
		$cacheManager = GetCacheManager();
		$re = $cacheManager->Get( 'getBestServiceMall_'.$count, CNS_INDEX );
		
		if( $re!= false )
			return $re;
			
		$sql="select id, shop_name, logo_url from shop_info where status=1 and plat_id>=100 order by service desc limit 0,".$count;
		return $this->db->GetPlan($sql, true, CNS_INDEX, 'getBestServiceMall_'.$count);
	}
	
	public function GetHotShop( $count = 12 )
	{
		return $this->GetHotShopAndMall( $count, 1 );
	}
	
	public function GetHotMall( $count = 12 )
	{
		return $this->GetHotShopAndMall( $count, 2 );
	}
	public function GetNewShop( $count = 12 )
	{
		$sql = "select id, shop_name, logo_url, review_count, score from shop_info where plat_id=1 order by id desc limit $count";
		$result = $this->db->GetPlan( $sql );
		return $result;
	}
	
	/*
	 * type = 1 : hot shop
	 * type = 2 : hot mall
	 */
	private function GetHotShopAndMall( $count, $type = 1 )
	{
		$cacheManager = GetCacheManager();
		$re = $cacheManager->Get( "getHotShopAndMall_".$count."_".$type, CNS_PRODUCT_SHOP_VARIABLE );
		
		if( $re != false )
			return $re;
		
		$searchEngine = GetShopSearch();
		$ids = $searchEngine->GetHotShopAndMall( $count, $type );
		if( $ids === false )
			return false;
			
		$sql = "select id, shop_name, logo_url, review_count, score from shop_info where id in(" . implode(',', $ids) . ")";
		
		return $this->SortByIdArray( $this->db->GetPlan($sql, true, CNS_PRODUCT_SHOP_VARIABLE, "getHotShopAndMall_".$count."_".$type), $ids );
	}
	
	function GetShopByKeyword( $keyword, $isMall, $page, $count )
    {
    	$searchEngine = GetShopSearch();
    	$resultIds = $searchEngine->GetShopByKeyWord( $keyword, $isMall, ($page-1) * $count, $count );
    	$result = $this->GetShopSimpleInfo( $resultIds );
    	$resultCount = count( $resultIds );
    	
    	return array( $resultCount, $result );
    }
	
    /**
     * 
     * @param type $sid
     * @return type 
     */
    function GetShopInfoTaobao( $sid ){
        $sql = "select * from shop_info_taobao where shop_id=".$sid;
		$result = $this->db->GetRow( $sql );
    	return $result;	 
    }
    
}

?>
